package com.testitems.config.security;

import com.testitems.enums.RoleEnum;
import lombok.Getter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;

import java.util.Collection;

/**
 * @author GFH
 * @since 2021-11-20
 */
public class UserInfo extends User {
    @Getter
    private Integer id;
    @Getter
    private String name;

    public UserInfo(String username, String password, Collection<? extends GrantedAuthority> authorities) {
        super(username, password, authorities);
    }

    public UserInfo(String username, String password, boolean enabled, boolean accountNonExpired, boolean credentialsNonExpired, boolean accountNonLocked, Collection<? extends GrantedAuthority> authorities, Integer id, String name) {
        super(username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities);
        this.id = id;
        this.name = name;
    }

    /**
     * 一个账户只有一个角色，直接判定是什么角色
     */
    public boolean hasRole(RoleEnum roleEnum) {
        final Collection<GrantedAuthority> authorities = super.getAuthorities();
        if (authorities == null || authorities.isEmpty()) {
            return false;
        }
        return authorities.contains(RoleHelper.roles.get(roleEnum));
    }
}
